package com.zst.demo4.controller;

import com.zsr.entities.CommonResult;
import com.zsr.entities.PermissionDTO;
import com.zst.demo4.service.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/zst/demo4")
@Slf4j
public class IndexController {

    @Autowired
    IPermissionService iPermissionService;

    @GetMapping("/index")
    public String index() {
        log.info("this is demo4 application.");
        return "this is demo4 application.";
    }

    @GetMapping("/get-permission/{id}")
    public CommonResult<PermissionDTO> getPermission(@PathVariable("id") Long id) {
        // 模拟错误
        if(id.equals(2L)){
            System.out.println(id/0);
        }
        //  调用角色信息
        Optional<PermissionDTO> permissionDTOOpt = iPermissionService.getPermissionById(id);
        if (!permissionDTOOpt.isPresent()) {
            return CommonResult.ResultSuccess(null);
        }
        PermissionDTO permissionDTO = permissionDTOOpt.get();
        return CommonResult.ResultSuccess(permissionDTO);
    }
}
